package com.ygo.feihua;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ygo.feihua.BmobTable.GuangGao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.ygo.feihua.util.*;

public class Adaptergg1 extends BaseAdapter
{

	List<GuangGao> data;
	Zujian zujian;
	Context context;
	int poo;
	int pmk;
	Gongju gj;
	DialogUtils du;
	
	public Adaptergg1(Context context ,List<GuangGao>data ){
		this.context=context;
		this.data=data;
		gj=Gongju.getdx(context);
		du=DialogUtils.getdx(context);
	
	}
	private class Zujian{
		TextView gg_yc,gg_lx,gg_nrlx,gg_name;
		ImageView gg_tp;
		Button gg_del,gg_fzlj;
	}
	
	public void setData(int position,GuangGao gg){
		data.set(position,gg);
		notifyDataSetChanged();
	}
	
	public void sx(List<GuangGao> data){
		this.data=data;
		notifyDataSetChanged();
	}
	
	public int getpo(){
		return poo;
	}

	public void addgg(GuangGao map){
		data.add(map);
		notifyDataSetChanged();
	}
	
	public void delgg(int po){
		data.remove(po);
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount()
	{
		// TODO: Implement this method
		return data.size();
	}

	@Override
	public Object getItem(int p1)
	{
		// TODO: Implement this method
		return data.get(p1);
	}

	@Override
	public long getItemId(int p1)
	{
		// TODO: Implement this method
		return p1;
	}

	@Override
	public View getView(final int position, View p2, ViewGroup p3)
	{
		if(p2==null){
			zujian=new Zujian();
			p2=LayoutInflater.from(context).inflate(R.layout.item_gg1,null);
			zujian.gg_yc=(TextView) p2.findViewById(R.id.gg_yc);
			zujian.gg_tp=(ImageView) p2.findViewById(R.id.gg_tp);
			zujian.gg_del=(Button) p2.findViewById(R.id.gg_del);
			zujian.gg_fzlj=(Button) p2.findViewById(R.id.gg_fzlj);
			zujian.gg_lx=(TextView) p2.findViewById(R.id.gg_lx);
			zujian.gg_name=(TextView) p2.findViewById(R.id.gg_name);
			zujian.gg_nrlx=(TextView) p2.findViewById(R.id.gg_nrlx);
			
			p2.setTag(zujian);
		}else{
			zujian=(Zujian) p2.getTag();
		}
		gj.tuxian(data.get(position).getGg_tp(),zujian.gg_tp);
		Integer yc=data.get(position).getGg_yc();
		Integer lx=data.get(position).getGg_lx();
		Integer nrlx=data.get(position).getGg_nrlx();
		String ycs="";
		String lxs="";
		String nrlxs="";
		switch(yc){
			case 0:
				ycs="不显示";
				break;
			case 1:
				ycs="显示到主页";
				break;
			case 2:
				ycs="显示到往期更新";
				break;
		}
		switch(lx){
			case 0:
				lxs="跳转网址";
				break;
			case 1:
				lxs="跳转帖子";
				break;
		}
		
		switch(nrlx){
			case 0:
				nrlxs="卡包广告";
				break;
			case 1:
				nrlxs="比赛广告";
				break;
			case 2:
				nrlxs="软件广告";
				break;
		}
		zujian.gg_name.setText(data.get(position).getGg_name());
		zujian.gg_nrlx.setText(nrlxs);
		zujian.gg_lx.setText(lxs);
		zujian.gg_yc.setText(ycs);
		/*zujian.gg_lj.setOnLongClickListener(new OnLongClickListener(){

				@Override
				public boolean onLongClick(View p1)
				{
					
					View[] v=du.dialoge(true,"修改链接","请输入链接");
					final EditText ed=(EditText) v[0];
					Button bt=(Button) v[1];
					bt.setText("确定");
					ed.setText((String)data.get(position).get("lj"));
					bt.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View vv)
							{
								du.dis();
								du.dialogj1(false,"","修改中,请稍等");

								String lj=ed.getText().toString().trim();
								GuangGao gg=new GuangGao();
								gg.setGg_wz(lj);
								gg.update((String)data.get(position).get("id"), new UpdateListener(){

										@Override
										public void done(BmobException p1)
										{
											if(p1==null){
												gj.show("修改成功");
												du.dis();
											}else{
												gj.show("修改失败,原因为"+p1);
												du.dis();
											}
											// TODO: Implement this method
										}
									});
								// TODO: Implement this method
							}
						});
					// TODO: Implement this method
					// TODO: Implement this method
					return true;
				}
		});
		*/
		
		zujian.gg_tp.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View vv)
				{
					poo=position;
					Intent i=new Intent(Intent.ACTION_PICK, null);
					i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
					//Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
					((Activity)context).startActivityForResult(i, 234);

					// TODO: Implement this method
				}
			});
		zujian.gg_fzlj.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					ClipboardManager cmb = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
					cmb.setText(data.get(position).getGg_wz());//复制命令
					gj.show("已复制到剪贴板");
					// TODO: Implement this method
				}
			});
		zujian.gg_del.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View vv)
				{
					View[] v=du.dialogt(false,"","确定删除该条广告？");
					Button b1,b2;
					b1=(Button) v[0];
					b2=(Button) v[1];
					b1.setText("取消");
					b2.setText("确定");
					b1.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								du.dis();
								// TODO: Implement this method
							}
						});

					b2.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View vv)
							{
								du.dis();
								du.dialogj1(false,"","加载中,请稍等");
								GuangGao gg=new GuangGao();
								gg.delete(data.get(position).getObjectId(), new UpdateListener(){

										@Override
										public void done(BmobException pp1)
										{
											if(pp1==null){
												gj.show("删除成功");
												delgg(position);
											}else{
												gj.show("删除失败");
											}
											du.dis();
											// TODO: Implement this method
										}
									});
								// TODO: Implement this method
							}
						});
					// TODO: Implement this method
				}
			});
		
		// TODO: Implement this method
		return p2;
	}
	
}
